package bb.lanxing.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import com.github.mikephil.charting.utils.Utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.os.StatFs;
import android.provider.MediaStore;
import android.text.TextUtils;

import bb.lanxing.App;
import bb.lanxing.activity.deeplink.DeepLinkPathConstants;
import bb.lanxing.common.config.Constants;
import bb.lanxing.databinding.unit.UnitConstants;
import gov.nist.core.Separators;

public class AppFileUtils {
    public static final int SIZETYPE_B = 1;
    public static final int SIZETYPE_KB = 2;
    public static final int SIZETYPE_MB = 3;
    public static final int SIZETYPE_GB = 4;
    private static String TAG = "FileUtils";

    public static boolean isSdCardAvailable() {
        return "mounted".equals(Environment.getExternalStorageState());
    }

    private static boolean ensureDirectoryExists(File dir) {
        return (dir.exists() && dir.isDirectory()) || dir.mkdirs();
    }

    public static boolean ensureDirectoryExists(String dir) {
        return ensureDirectoryExists(new File(dir));
    }

    public static String buildExternalDirectoryPath(String... components) {
        StringBuilder sb = new StringBuilder();
        String str = null;
        sb.append(App.getContext().getExternalFilesDir(null));
        sb.append(File.separatorChar);
        sb.append(DeepLinkPathConstants.SCHEME_APP_LANXING);
        for (String str2 : components) {
            sb.append(File.separatorChar);
            sb.append(str2);
        }
        String sb2 = sb.toString();
        if (ensureDirectoryExists(sb2)) {
            str = sb2;
        } else {
            String builderMntDirectoryPath = builderMntDirectoryPath(components);
            if (ensureDirectoryExists(builderMntDirectoryPath)) {
                str = builderMntDirectoryPath;
            }
        }
        Log.d("fileutils buildExternalDirectoryPath path = " + str);
        return str;
    }

    private static String builderMntDirectoryPath(String... components) {
        StringBuilder sb = new StringBuilder();
        sb.append(File.separatorChar);
        sb.append("mnt");
        sb.append(File.separatorChar);
        sb.append("sdcard");
        sb.append(File.separatorChar);
        sb.append(DeepLinkPathConstants.SCHEME_APP_LANXING);
        for (String str : components) {
            sb.append(File.separatorChar);
            sb.append(str);
        }
        return sb.toString();
    }

    public static String buildSystemCameraDirectoryPath(String dir) {
        String absolutePath =
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + File.separatorChar + dir)
                        .getAbsolutePath();
        if (!ensureDirectoryExists(absolutePath)) {
            absolutePath = "/mnt/sdcard/DCIM" + File.separatorChar + dir;
            if (!ensureDirectoryExists(absolutePath)) {
                absolutePath = null;
                App.getContext().showMessage("操作失败，BlueRider无法在SD卡创建文件。");
            }
        }
        Log.d("fileutils buildSystemCameraDirectoryPath path = " + absolutePath);
        return absolutePath;
    }

    public static void createNomediaFile(File dir) throws IOException {
        if (dir == null || !dir.isDirectory()) {
            return;
        }
        File file = new File(dir, ".nomedia");
        if (file.exists()) {
            return;
        }
        file.createNewFile();
    }

    public static void saveImage(Bitmap b, String filePath) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);
        b.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    }

    public static void saveImage(Bitmap b, String filePath, Bitmap.CompressFormat format, int quality)
            throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);
        b.compress(format, quality, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    }

    public static String saveBitmapToDCIM(Context context, Bitmap bitmap, String bitName) {
        String str;
        String str2 = Build.BRAND;
        if (str2.equals("xiaomi"/* AssistUtils.BRAND_XIAOMI */)) {
            str = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + bitName;
        } else if (str2.equalsIgnoreCase("Huawei")) {
            str = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + bitName;
        } else {
            str = Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + bitName;
        }
        if (Build.VERSION.SDK_INT >= 29) {
            saveSignImage(bitName, bitmap, context);
            return str;
        }
        Log.v("saveBitmap brand", " " + str2);
        File file = new File(str);
        if (file.exists()) {
            file.delete();
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            if (bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream)) {
                fileOutputStream.flush();
                fileOutputStream.close();
                if (Build.VERSION.SDK_INT >= 29) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("_data", file.getAbsolutePath());
                    contentValues.put("mime_type", "image/jpeg");
                    context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
                } else {
                    MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), bitName,
                            (String) null);
                }
            }
            return str;
        } catch (FileNotFoundException e) {
            Log.e("FileNotFoundException", "FileNotFoundException:" + e.getMessage().toString());
            e.printStackTrace();
            return null;
        } catch (IOException e2) {
            Log.e("IOException", "IOException:" + e2.getMessage().toString());
            e2.printStackTrace();
            return null;
        } catch (Exception e3) {
            Log.e("IOException", "IOException:" + e3.getMessage().toString());
            e3.printStackTrace();
            return null;
        }
    }

    public static void saveSignImage(String fileName, Bitmap bitmap, Context context) {
        OutputStream openOutputStream;
        try {
            ContentValues contentValues = new ContentValues();
            contentValues.put("_display_name", fileName);
            if (Build.VERSION.SDK_INT >= 29) {
                contentValues.put("relative_path", "DCIM/");
            } else {
                contentValues.put("_data",
                        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath());
            }
            contentValues.put("mime_type", "image/JPEG");
            Uri insert =
                    context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
            if (insert == null || (openOutputStream = context.getContentResolver().openOutputStream(insert)) == null) {
                return;
            }
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, openOutputStream);
            openOutputStream.flush();
            openOutputStream.close();
        } catch (Exception unused) {
        }
    }

    public static byte[] encryptGZIP(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);
            gZIPOutputStream.write(str.getBytes(StandardCharsets.UTF_8));
            gZIPOutputStream.close();
            byteArrayOutputStream.flush();
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            byteArrayOutputStream.close();
            return byteArray;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存字符串到文件
     *
     * @param str
     * @param filePath
     * @return
     */
    public static String buildGZIPFile(String str, String filePath) {
        if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(filePath)) {
            File file = new File(filePath);
            try {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                gZIPOutputStream.write(str.getBytes(StandardCharsets.UTF_8));
                gZIPOutputStream.close();
                byteArrayOutputStream.flush();
                byte[] byteArray = byteArrayOutputStream.toByteArray();
                fileOutputStream.write(byteArray);
                fileOutputStream.flush();
                fileOutputStream.close();
                byteArrayOutputStream.close();
                Log.d(Constants.TAG, "path = " + file.getPath() + " size = " + file.length());
                return CommonUtil.getMD5(byteArray);
            } catch (Exception e) {
                e.printStackTrace();
                if (file.exists()) {
                    file.delete();
                }
            }
        }
        return null;
    }

    public static Writer getGZIPWriter(String outPath) throws IOException {
        return new BufferedWriter(
                new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(outPath)), StandardCharsets.UTF_8));
    }

    public static String decryptGZIP(String filePath) {
        if (filePath != null && !"".equals(filePath)) {
            try {
                FileInputStream fileInputStream = new FileInputStream(filePath);
                GZIPInputStream gZIPInputStream = new GZIPInputStream(fileInputStream);
                byte[] bArr = new byte[1024];
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                while (true) {
                    int read = gZIPInputStream.read(bArr, 0, 1024);
                    if (read != -1) {
                        byteArrayOutputStream.write(bArr, 0, read);
                    } else {
                        gZIPInputStream.close();
                        byteArrayOutputStream.flush();
                        byte[] byteArray = byteArrayOutputStream.toByteArray();
                        byteArrayOutputStream.close();
                        fileInputStream.close();
                        return new String(byteArray, StandardCharsets.UTF_8);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String decryptGZIP(byte[] datas) {
        if (datas != null && datas.length != 0) {
            try {
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(datas);
                GZIPInputStream gZIPInputStream = new GZIPInputStream(byteArrayInputStream);
                byte[] bArr = new byte[1024];
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                while (true) {
                    int read = gZIPInputStream.read(bArr, 0, 1024);
                    if (read != -1) {
                        byteArrayOutputStream.write(bArr, 0, read);
                    } else {
                        gZIPInputStream.close();
                        byteArrayOutputStream.flush();
                        byte[] byteArray = byteArrayOutputStream.toByteArray();
                        byteArrayOutputStream.close();
                        byteArrayInputStream.close();
                        return new String(byteArray, StandardCharsets.UTF_8);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String readFile(String filePath) {
        String str = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(filePath);
            byte[] bArr = new byte[fileInputStream.available()];
            fileInputStream.read(bArr);
            String str2 = new String(bArr, StandardCharsets.UTF_8);
            try {
                fileInputStream.close();
                return str2;
            } catch (Exception e) {
                e = e;
                str = str2;
                e.printStackTrace();
                return str;
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        return str;
    }

    public static String getFileFromSD(String path) {
        String str = "";
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    break;
                }
                str = str + readLine;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String getFileFromAssets(String filePath) {
        try {
            InputStream open = App.getContext().getAssets().open(filePath);
            byte[] bArr = new byte[open.available()];
            open.read(bArr);
            open.close();
            return new String(bArr, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static InputStream getInputStreamFromAssets(String filePath) {
        try {
            return App.getContext().getAssets().open(filePath);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void writeFile(String strWrite, String filePath) {
        writeFile(strWrite.getBytes(), filePath);
    }

    public static void writeFile(byte[] bytes, String filePath) {
        try {
            deleteFile(filePath);
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            fileOutputStream.write(bytes);
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean writeFileWithCallBack(String strWrite, String filePath) {
        return writeFileWithCallBack(strWrite.getBytes(), filePath);
    }

    public static boolean writeFileWithCallBack(byte[] bytes, String filePath) {
        try {
            deleteFile(filePath);
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            fileOutputStream.write(bytes);
            fileOutputStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static long copyFile(File srcFile, File destDir, String newFileName) {
        if (!srcFile.exists()) {
            System.out.println("源文件不存在");
            return -1L;
        } else if (!destDir.exists()) {
            System.out.println("目标目录不存在");
            return -1L;
        } else if (newFileName == null) {
            System.out.println("文件名为null");
            return -1L;
        } else {
            try {
                FileChannel channel = new FileInputStream(srcFile).getChannel();
                FileChannel channel2 = new FileOutputStream(new File(destDir, newFileName)).getChannel();
                long size = channel.size();
                channel.transferTo(0L, channel.size(), channel2);
                channel.close();
                channel2.close();
                return size;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return 0L;
            } catch (IOException e2) {
                e2.printStackTrace();
                return 0L;
            }
        }
    }

    public static boolean deleteFile(String filePath) {
        File file = new File(filePath);
        if (file.isFile() && file.exists()) {
            return file.delete();
        }
        return false;
    }

    public static boolean cleanDirectory(String fileDir) {
        if (!fileDir.endsWith(File.separator)) {
            fileDir = fileDir + File.separator;
        }
        File file = new File(fileDir);
        if (file.exists() && file.isDirectory()) {
            File[] listFiles = file.listFiles();
            boolean z = true;
            for (int i = 0; i < listFiles.length
                    && (!listFiles[i].isFile() || (z = deleteFile(listFiles[i].getAbsolutePath()))); i++) {
            }
            return z;
        }
        return false;
    }

    public static String parseFilePath(Context context, Uri uri) {
        Cursor query;
        if (uri == null) {
            return null;
        }
        String scheme = uri.getScheme();
        if (scheme == null) {
            return uri.toString();
        }
        if (scheme.equals(DeepLinkPathConstants.SCHEME_FILE)) {
            return uri.getPath();
        }
        if (scheme.equals("content") && (query =
                context.getContentResolver().query(uri, new String[] { "_data" }, null, null, null)) != null) {
            try {
                if (query.moveToFirst()) {
                    return query.getString(0);
                }
            } finally {
                query.close();
            }
        }
        return null;
    }

    public static boolean isFileExists(String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return false;
        }
        return isFileExists(new File(filePath));
    }

    public static boolean isFileExists(File file) {
        return file != null && file.exists();
    }

    public static String getName(String path) {
        int lastIndexOf;
        int i;
        return (path == null || (lastIndexOf = path.lastIndexOf(File.separator)) == -1
                || (i = lastIndexOf + 1) >= path.length()) ? "" : path.substring(i, path.length());
    }

    public static String getNoRepeatFilePath(final String path) {
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        return !new File(path).exists() ? path : getNoRepeatFile(path, 1);
    }

    private static String getNoRepeatFile(final String path, int i) {
        String str;
        String str2;
        int lastIndexOf = path.lastIndexOf(".");
        if (lastIndexOf != -1) {
            str = path.substring(lastIndexOf);
            str2 = path.substring(0, lastIndexOf);
        } else {
            str = "";
            str2 = path;
        }
        File file = new File(str2 + Separators.LPAREN + i + Separators.RPAREN + str);
        if (!file.exists()) {
            return file.getAbsolutePath();
        }
        return getNoRepeatFile(path, i + 1);
    }

    public static Uri fileScanVideo(Context context, String videoPath, int videoWidth, int videoHeight, int videoTime) {
        Uri insert = null;
        File file = new File(videoPath);
        if (file.exists()) {
            long length = file.length();
            String name = file.getName();
            long currentTimeMillis = System.currentTimeMillis();
            ContentValues contentValues = new ContentValues(11);
            contentValues.put("title", name);
            contentValues.put("width", videoWidth);
            contentValues.put("height", videoHeight);
            contentValues.put("_size", length);
            contentValues.put("duration", videoTime * 1000);
            contentValues.put("datetaken", currentTimeMillis);
            contentValues.put("_display_name", name);
            long j = currentTimeMillis / 1000;
            contentValues.put("date_modified", j);
            contentValues.put("date_added", j);
            contentValues.put("mime_type", "video/mp4");
            ContentResolver contentResolver = context.getContentResolver();
            if (contentResolver != null) {
                try {
                    if (Build.VERSION.SDK_INT >= 29) {
                        contentValues.put("relative_path", Environment.DIRECTORY_MOVIES + "/Xoss/ReAr");
                        insert = contentResolver.insert(MediaStore.Video.Media.getContentUri("external_primary"),
                                contentValues);
                    } else {
                        contentValues.put("_data", videoPath);
                        insert = contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (insert != null) {
                    if (Build.VERSION.SDK_INT >= 29) {
                        try {
                            ParcelFileDescriptor openFileDescriptor =
                                    contentResolver.openFileDescriptor(insert, UnitConstants.UNIT_POWER);
                            FileOutputStream fileOutputStream =
                                    new FileOutputStream(openFileDescriptor.getFileDescriptor());
                            FileInputStream fileInputStream = new FileInputStream(new File(videoPath));
                            byte[] bArr = new byte[8192];
                            while (true) {
                                int read = fileInputStream.read(bArr);
                                if (read <= 0) {
                                    break;
                                }
                                fileOutputStream.write(bArr, 0, read);
                            }
                            fileOutputStream.close();
                            fileInputStream.close();
                            openFileDescriptor.close();
                        } catch (Exception e2) {
                            e2.printStackTrace();
                        }
                        contentValues.clear();
                        contentValues.put("is_pending", (Integer) 0);
                        contentResolver.update(insert, contentValues, null, null);
                        deleteFile(videoPath);
                    } else {
                        refreshAlbumByMediaScannerConnectionMP4(context, videoPath);
                    }
                }
                return insert;
            }
            // insert = null;
            // if (insert != null) {
            // }
            return insert;
        }
        return null;
    }

    public static void refreshAlbumByMediaScannerConnectionMP4(Context context, String path) {
        MediaScannerConnection.scanFile(context, new String[] { path }, new String[] { "video/mp4" },
                new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public final void onScanCompleted(String str, Uri uri) {
                        // FileUtils.lambda$refreshAlbumByMediaScannerConnectionMP4$0(str, uri);
                    }
                });
    }

    public static boolean isSDExists() {
        return Environment.getExternalStorageState().equals("mounted");
    }

    public static long getSDFreeMemory() {
        try {
            if (isSDExists()) {
                StatFs statFs = new StatFs(App.getContext().getExternalFilesDir(null).getPath());
                return statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong();
            }
            return 0L;
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    public static void saveStringToFile(String text, String filePath) throws IOException {
        saveStreamToFile(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)), filePath);
    }

    public static synchronized void saveStreamToFile(InputStream in2, String filePath) {
        synchronized (AppFileUtils.class) {
            try {
                File file = new File(filePath);
                if (!file.exists()) {
                    File parentFile = file.getParentFile();
                    if (!parentFile.exists()) {
                        parentFile.mkdirs();
                    }
                    file.createNewFile();
                }
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                copyStream(in2, fileOutputStream);
                fileOutputStream.close();
            } catch (Exception e) {
                Log.e("saveStreamToFile", e.getMessage());
            }
        }
    }

    public static void copyStream(InputStream in2, OutputStream out) throws IOException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(in2);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
        byte[] bArr = new byte[4096];
        while (true) {
            int read = bufferedInputStream.read(bArr);
            if (read != -1) {
                bufferedOutputStream.write(bArr, 0, read);
            } else {
                bufferedOutputStream.flush();
                return;
            }
        }
    }

    public static double getFileOrFilesSize(String filePath, int sizeType) {
        long j;
        File file = new File(filePath);
        try {
            if (file.isDirectory()) {
                j = getFileSizes(file);
            } else {
                j = getFileSize(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "获取文件大小失败!");
            j = 0;
        }
        return FormetFileSize(j, sizeType);
    }

    private static double FormetFileSize(long fileS, int sizeType) {
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
        decimalFormatSymbols.setDecimalSeparator('.');
        decimalFormat.setDecimalFormatSymbols(decimalFormatSymbols);
        if (sizeType != SIZETYPE_B) {
            if (sizeType != SIZETYPE_KB) {
                if (sizeType != SIZETYPE_MB) {
                    return sizeType != SIZETYPE_GB ? Utils.DOUBLE_EPSILON
                            : Double.parseDouble(decimalFormat.format(fileS / 1.073741824E9d));
                }
                return Double.parseDouble(decimalFormat.format(fileS / 1048576.0d));
            }
            return Double.parseDouble(decimalFormat.format(fileS / 1024.0d));
        }
        return Double.parseDouble(decimalFormat.format(fileS));
    }

    private static long getFileSizes(File f) throws Exception {
        long fileSize;
        File[] listFiles = f.listFiles();
        long j = 0;
        for (int i = 0; i < listFiles.length; i++) {
            if (listFiles[i].isDirectory()) {
                fileSize = getFileSizes(listFiles[i]);
            } else {
                fileSize = getFileSize(listFiles[i]);
            }
            j += fileSize;
        }
        return j;
    }

    private static long getFileSize(File file) throws Exception {
        if (file.exists()) {
            return new FileInputStream(file).available();
        }
        file.createNewFile();
        Log.e(TAG, "获取文件大小不存在!");
        return 0L;
    }

    /*
     * 删除过期日志文件
     *
     * @param days 保留日志文件天数
     *
     * @param path 日志文件路径
     */
    public static void deleteOldFile(int days, String path) {
        File dir = new File(path);
        if (!dir.exists() || !dir.isDirectory() || dir.listFiles() == null) {
            return;
        }
        for (File file : dir.listFiles()) {
            if (!file.isFile()) {
                continue;
            }
            long time = com.blankj.utilcode.util.FileUtils.getFileLastModified(file);
            long diff = System.currentTimeMillis() - time;
            long diffDays = diff / (24L * 60 * 60 * 1000);
            if (diffDays >= days) {
                file.delete();
            }
        }
    }

}
